<template>
  <div>
    <el-tree
      ref="refTree"
      :data="list"
      :props="{ label: 'name' }"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />

    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="medium" @click="hClose">取消</el-button>
        <el-button
          size="medium"
          type="primary"
          @click="hSubmit"
        >确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permId: [],
      list: []
    }
  },
  created() {
    this.loadAssign()
    this.loadAssignPer()
  },
  methods: {
    async loadAssign() {
      const res = await getPermissionList()
      console.log(res)
      this.list = tranListToTreeData(res.data)
    },
    async loadAssignPer() {
      //   console.log(this.curId)
      const res = await getRoleDetail(this.curId)
      console.log(111, res)
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    },
    hClose() {
      this.$emit('close')
    },
    async hSubmit() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      const res = await assignPerm({ id: this.curId, permIds: permIds })
      console.log(res)
      this.$message.success('成功')
      this.$emit('close')
    }
  }
}
</script>
